import { assign } from 'lodash';
import { AnyAction } from 'redux';
import ACTIONS from 'constants/review';

const initialState: Record<string, any> = {
  levelTotal: 0,
  levels: [],
  wordTotal: 0,
  words: [],
  testTotal: 0,
  tests: [],
};

export default (state = initialState, action: AnyAction) => {
  switch (action.type) {
    case ACTIONS.FETCH_LEVEL_LIST_SUCCESS:
      return assign({}, state, {
        levels: action.response.data.items,
        levelTotal: action.response.data.total,
      });

    case ACTIONS.FETCH_WORD_LIST_SUCCESS:
      return assign({}, state, {
        words: action.response.data.items,
        wordTotal: action.response.data.total,
      });

    case ACTIONS.FETCH_TEST_LIST_SUCCESS:
      return assign({}, state, {
        tests: action.response.data.items,
        testTotal: action.response.data.total,
      });

    default:
      return state;
  }
};
